//
//  STPopMenuViewController.m
//  GrapeGold
//
//  Created by Mac on 2018/5/7.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "STPopMenuViewController.h"

@interface STPopMenuViewController ()<UIPopoverPresentationControllerDelegate>
@property(nonatomic, strong) NSMutableArray                     *buttonsArray;/**< 数组 */
@property(nonatomic, strong) NSMutableArray                     *lineArray;/**< 线数组 */
@property(nonatomic, strong) UIButton                          *clearBut;/**< 背景 */
@property(nonatomic, strong) UIView                               *customView;/**< customView */
@property(nonatomic, strong) UIImageView                         *arrowsImageView;/**< 箭头图片 */
@end

@implementation STPopMenuViewController
- (instancetype)initWithSize:(CGSize)size targetView:(UIView *)targetView{
    if (self == [super init]){
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
        UIWindow * window = UIApplication.sharedApplication.delegate.window;
        
        self.clearBut = [[UIButton alloc] initWithFrame:window.bounds];
        self.clearBut.backgroundColor  = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        [self.clearBut addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];
        [window addSubview:self.clearBut];
        
        CGRect reallReact = [targetView convertRect:targetView.bounds toView:self.view.window];
        
        UIView * reallView = [[UIView alloc] initWithFrame:reallReact];
        
        CGFloat left = reallView.right - size.width;
        CGFloat top = reallView.bottom ;
        
        UIImage * arrowImage = [UIImage imageNamed:@"STTools.bundle/STImages/arrow_up@3x.png"];
        self.arrowsImageView = [[UIImageView alloc] initWithImage:arrowImage];
        self.arrowsImageView.top = top + 3;
        self.arrowsImageView.right = reallView.right - 10;
        [self.clearBut addSubview:self.arrowsImageView];
        
        
        self.customView = [[UIView alloc] initWithFrame:CGRectMake(left, self.arrowsImageView.bottom - 5, size.width, size.height)];
        self.customView.layer.shadowOffset = CGSizeMake(1.5, 1.5);
        self.customView.layer.shadowColor = [UIColor grayColor].CGColor;
        self.customView.layer.shadowRadius = 1;
        self.customView.layer.shadowOpacity = 1.0;
        CGRect shadowFrame = self.customView.layer.bounds;
        CGPathRef shadowPath = [UIBezierPath
                                bezierPathWithRect:shadowFrame].CGPath;
        self.customView.layer.shadowPath = shadowPath;
        self.customView.backgroundColor = UIColor.whiteColor;
        [self.clearBut addSubview:self.customView];
        
        
    }
    return self;
}
- (void)setTitlesArray:(NSArray<NSString *> *)titlesArray{
    _titlesArray = titlesArray;
    [self configSubView];
}
- (void)setTitleColor:(UIColor *)titleColor{
    _titleColor = titleColor;
    for (UIButton * buttton in self.buttonsArray) {
        [buttton setTitleColor:titleColor forState:UIControlStateNormal];
    }
}
- (void)setLineColor:(UIColor *)lineColor{
    _lineColor = lineColor;
    for (UIView * line in self.lineArray) {
        line.backgroundColor = lineColor;
    }
}
- (void)setItemBackgourndColor:(UIColor *)itemBackgourndColor{
    _itemBackgourndColor = itemBackgourndColor;
    for (UIButton * buttton in self.buttonsArray) {
        [buttton setBackgroundColor:itemBackgourndColor];
    }
}
- (void)setItemTitleColor:(UIColor *)color atIndex:(NSInteger)index{
    if (self.buttonsArray) {
        UIButton * button = self.buttonsArray[index];
        [button setTitleColor:color forState:UIControlStateNormal];
    }
    
}
#pragma mark --subView
- (void)configSubView{
    // self.view.backgroundColor = FlatOrange;
    for (UIButton * button in self.buttonsArray) {
        [button removeFromSuperview];
    }
    for (UIView * line in self.lineArray) {
        [line removeFromSuperview];
    }
    self.lineArray = [NSMutableArray new];
    self.buttonsArray = [NSMutableArray new];
    CGFloat buttonWith = self.customView.width;
    CGFloat buttonHeight = self.customView.height / self.titlesArray.count;
    CGFloat top = 0;
    for (NSInteger i = 0 ; i < self.titlesArray.count; i ++) {
        NSString * title = self.titlesArray[i];
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, top, buttonWith, buttonHeight);
        [button setTitle:title forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(onSelctedItemButton:) forControlEvents:UIControlEventTouchUpInside];
        if (self.titleColor) {
            [button setTitleColor:self.titleColor forState:UIControlStateNormal];
        }
        if (self.itemBackgourndColor) {
            button.backgroundColor = self.itemBackgourndColor;
        }
        [self.customView addSubview:button];
        [self.buttonsArray addObject:button];
        UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, buttonHeight -0.5, buttonWith, 0.5)];
        if (self.lineColor) {
            line.backgroundColor = self.lineColor;
        }
        [button addSubview:line];
        [self.lineArray addObject:line];
        top = i * buttonHeight +  buttonHeight;
        
    }
}

#pragma mark --Action Method
- (void)clearAction:(UIButton*)sender{
    [self.clearBut removeFromSuperview];
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}
- (void)onSelctedItemButton:(UIButton*)sender{
    NSInteger index = [self.buttonsArray indexOfObject:sender];
    NSString *title = sender.currentTitle;
    __weak typeof(self) weakSelf =  self;
    [self.clearBut removeFromSuperview];
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.onSelctedItemHandle) {
            self.onSelctedItemHandle(weakSelf, index, title);
        }
        
    }];
    
}
#pragma mark --Private Method
- (void)show{
    
}
@end

